//package com.realms;
//
//import com.pojo.User;
//import com.service.UserService;
//import com.utils.MD5Util;
//import com.utils.ShiroToken;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.springframework.beans.factory.annotation.Autowired;
//
//public class ShiroRealm extends AuthorizingRealm {
//    @Autowired
//    private UserService userService;
//
//    //授权
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        String username = (String)principalCollection.getPrimaryPrincipal();
//
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        authorizationInfo.setRoles(userService.findRoleByUserName(username));
//        authorizationInfo.setStringPermissions(userService.findPermissionByUserName(username));
//        return authorizationInfo;
//    }
//
//    //登录
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        ShiroToken token = (ShiroToken) authenticationToken;
//        String username = token.getUsername();
//        String password = token.getPswd();
//        User user = userService.findByNameAndPwd(username, MD5Util.md5(password));
//        if(user == null) {
//            throw new UnknownAccountException("账号或密码错误");//没找到帐号
//        }
//        if(user.getStatus() == 1) {
//            throw new LockedAccountException("该账户已被锁定"); //帐号锁定
//        }
//        //第一、二个参数，会去与token里名字一样的参数作比较检验是否相等。
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
//                user.getUsername(), //用户名
//                user.getPassword().toCharArray(), //密码
//                getName()  //realm name
//        );
//        return authenticationInfo;
//    }
//
//    @Override
//    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
//        super.clearCachedAuthorizationInfo(principals);
//    }
//
//    @Override
//    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
//        super.clearCachedAuthenticationInfo(principals);
//    }
//
//    @Override
//    public void clearCache(PrincipalCollection principals) {
//        super.clearCache(principals);
//    }
//
//    public void clearAllCachedAuthorizationInfo() {
//        getAuthorizationCache().clear();
//    }
//
//    public void clearAllCachedAuthenticationInfo() {
//        getAuthenticationCache().clear();
//    }
//
//    public void clearAllCache() {
//        clearAllCachedAuthenticationInfo();
//        clearAllCachedAuthorizationInfo();
//    }
//
//}
